package com.liuyang.service.impl;



import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuyang.entity.Destination;
import com.liuyang.entity.MaterialManagement;
import com.liuyang.entity.Materials;
import com.liuyang.entity.Sourec;
import com.liuyang.mapper.DestinationMapper;
import com.liuyang.mapper.MaterialManagementMapper;
import com.liuyang.mapper.MaterialsMapper;
import com.liuyang.mapper.SourceMapper;
import com.liuyang.service.MaterialManagementService;
import com.liuyang.vo.InsterMaterialManagementVo;
import com.liuyang.vo.UpdateMaterialManagement;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;


@Service
public class MaterialManagementServiceImpl extends ServiceImpl<MaterialManagementMapper, MaterialManagement> implements MaterialManagementService {

    @Resource
    private MaterialManagementMapper mapper;
    @Resource
    private SourceMapper sourceMapper;
    @Resource
    private DestinationMapper destination;
    @Resource
    private MaterialsMapper materialsMapper;
    /**
     * 入库操作
     * @param vo
     * @return
     */
    @Override
    public String warehousing(InsterMaterialManagementVo vo) {
        //判断参数是否为空
        if (vo == null){
            return "参数为空，请重试";
        }
        //入库时需要判断库里面有没有，如果有就直接在原本数量的基础上进行添加，还要注意过期时间是否需要更新
        //查询出仓库表中这个物资名字的所有的数据
        MaterialManagement management = mapper.selectByName(vo.getMaterialsName());

        //添加到入库表
        if (StringUtils.isEmpty(management)){
            //没有直接添加即可
            //生成id
            UUID uuid = UUID.randomUUID();
            String id = uuid.toString();
            MaterialManagement materialManagement =new MaterialManagement();
            materialManagement.setWareHouseId(id);
            materialManagement.setMaterialsName(vo.getMaterialsName());
            materialManagement.setMaterialsNumber(vo.getMaterialsNumber());
            materialManagement.setMaterialsType(vo.getMaterialsType());
            materialManagement.setExpireTime(vo.getExpireTime());
            int insert = mapper.insert(materialManagement);
            if (insert == 0){
                return "仓库入库失败，请重试";
            }
        }else {
            //如果有旧的过期时间，就以旧的过期时间算，不进行替换
            //将库里的总数和要添加的数量加在一起填回数据库
            Integer materialsNumber = management.getMaterialsNumber();
            Integer number = vo.getMaterialsNumber();
            management.setMaterialsNumber(materialsNumber+number);
            boolean equals = management.getMaterialsName().equals(vo.getMaterialsName());
            if (!equals){
                return "物资名字重复请重试";
            }
            int insert = mapper.updateByOne(management);
            if (insert == 0){
                return "仓库入库失败，请重试";
            }
        }
        Sourec sourec=new Sourec();
        String id = UUID.randomUUID().toString();
        sourec.setSourceId(id);
        sourec.setMaterialsName(vo.getMaterialsName());
        sourec.setMaterialsNumber(vo.getMaterialsNumber());
        sourec.setMaterialsSource(vo.getMaterialsSource());
        sourec.setMaterialsType(vo.getMaterialsType());
        int insert1 = sourceMapper.insert(sourec);
        if (insert1 == 0){
            return "入库信息登记失败，请重试";
        }
        return "入库成功";
    }

    @Override
    public String outbound(UpdateMaterialManagement updateMaterialManagement) {
        //首先判断入参是否为空
        if (StringUtils.isEmpty(updateMaterialManagement)){
            return "参数为空请重试";
        }
        Materials materials = materialsMapper.selectById(updateMaterialManagement.getMaterialsId());
        if (!materials.getStatus().equals("1")){
            return "审核还未通过，请等审核通过在出库";
        }
        //查出总库的数据，进行更改，然后再添加一个出库数据
        MaterialManagement materialManagement = mapper.selectByName(updateMaterialManagement.getMaterialsName());
        if (StringUtils.isEmpty(materialManagement)){
            return "总库里面没有该物资，请重试";
        }
        Integer materialsNumber = materialManagement.getMaterialsNumber();
        Integer materialsNumber1 = updateMaterialManagement.getMaterialsNumber();
        int i = materialsNumber - materialsNumber1;
        if (i<0){
            return "库里该物资总数不够调用，请联系管理员";
        }
        materialManagement.setMaterialsNumber(i);
        Integer integer = mapper.updateByOne(materialManagement);
        if (integer ==0){
            return "数据更新失败";
        }
        Destination ds =new Destination();
        String id = UUID.randomUUID().toString();
        ds.setDestinationId(id);
        ds.setMaterialsName(updateMaterialManagement.getMaterialsName());
        ds.setMaterialsDestination(updateMaterialManagement.getMaterialsDestination());
        ds.setMaterialsNumber(updateMaterialManagement.getMaterialsNumber());
        ds.setMaterialsType(updateMaterialManagement.getMaterialsType());
        int insert = destination.insert(ds);
        if (insert==0){
            return "新增出库记录失败，请重试";
        }
        return "出库成功";
    }

    /**
     * 修改过期时间
     * @param expirtTime
     * @return
     */
    @Override
    public String updateByTime(String expirtTime,String materialsName) {
        if (StringUtils.isEmpty(expirtTime) || StringUtils.isEmpty(materialsName)){
            return "参数为空，请重试";
        }
        MaterialManagement materialManagement = mapper.selectByName(materialsName);
        if (StringUtils.isEmpty(materialManagement)){
            return "没有查到相关数据，请重试";
        }
        materialManagement.setExpireTime(expirtTime);
        Integer integer = mapper.updateByOne(materialManagement);
        if (integer==0){
            return "修改过期时间失败";
        }
        return "修改过期时间成功";
    }

    @Override
    public List<MaterialManagement> getAll(Integer page, Integer pageSize, String type) {
        //查询仓库表的数据
        page=page-1;
        List<MaterialManagement> materialManagements = mapper.selectListPage(page, pageSize, type);
        if (CollectionUtils.isEmpty(materialManagements)){
            return null;
        }
        //判断库存量
//        for (MaterialManagement materialManagement : materialManagements) {
//            if (materialManagement.getMaterialsNumber()<50){
//                throw new RuntimeException(materialManagement.getMaterialsName()+"库存不足啦需要补充");
//            }
//        }
        return materialManagements;
    }

    @Override
    public List<MaterialManagement> getBySource(Integer page, Integer pageSize, String type) {
        page=page-1;
        List<MaterialManagement> materialManagements = sourceMapper.selectListPage(page, pageSize, type);
        if (CollectionUtils.isEmpty(materialManagements)){
            return null;
        }
        return materialManagements;
    }

    @Override
    public List<MaterialManagement> getByDestination(Integer page, Integer pageSize, String type) {
        page=page-1;
        List<MaterialManagement> materialManagements = destination.selectListPage(page, pageSize, type);
        if (CollectionUtils.isEmpty(materialManagements)){
            return null;
        }
        return materialManagements;
    }
}
